 package com.txby.platformApi.component.service;

import java.util.Date;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.jck.mybatis.mapper.MybatisBaseDaoCRUD;
import com.txby.pf.common.utils.R;
import com.txby.platformCore.entity.shop.GoodsPic;
import com.txby.platformCore.entity.shop.GoodsPicRealation;
import com.txby.platformCore.entity.shop.GoodsStockNum;
import com.txby.platformCore.entity.shop.NrsGoods;
import com.txby.platformCore.entity.shop.OrderMealGoods;
import com.txby.platformCore.entity.shop.OrderMealGoodsRelation;

@Service
public class WebOrderMealGoodsService extends MybatisBaseDaoCRUD {
 
	@Resource(name = "mybatisBaseDaoCRUD")
	public MybatisBaseDaoCRUD mBaseDao;
	/**
	 * 新增普通商品
	 * @param OrderMealGoods
	 * @return
	 */
	@Transactional(propagation = Propagation.REQUIRED,timeout=36000,rollbackFor=Exception.class)
	public R saveOrderMealGoods(OrderMealGoods goods,Map m)  {
			GoodsPic goodsPic = new GoodsPic();
			goodsPic.setUrl(m.get("url")+"");
			saveObject(goods);
			Long id=goods.getId_();
			saveObject(goodsPic);
			Long picId=goodsPic.getId_();
			
			GoodsPicRealation goodsPicRealation = new GoodsPicRealation();
			goodsPicRealation.setGoodsId(id);
			goodsPicRealation.setGoodsPicId(picId);
			goodsPicRealation.setGoodsTypeId(3l);;
			saveObject(goodsPicRealation);
			GoodsStockNum goodsStockNum = new GoodsStockNum();
			goodsStockNum.setGoodsTypeId(3l);
			goodsStockNum.setGoodsId(id);
			goodsStockNum.setShopsId(goods.getShopsId());
			goodsStockNum.setStockNum(goods.getStockNum());
			goodsStockNum.setResidueNum(goods.getStockNum());
			goodsStockNum.setOrdersOutNum(0L);
			saveObject(goodsStockNum);
			java.util.List<String> mealType = JSON.parseArray(m.get("mealType")+"", String.class) ;
			java.util.List<String> mealWeek = JSON.parseArray(m.get("mealWeek")+"", String.class) ;
			for(String mt:mealType) {
				for(String mw:mealWeek) {
					OrderMealGoodsRelation o = new OrderMealGoodsRelation();
					o.setCreater(goods.getCreater());
					o.setUpdater(goods.getCreater());
					o.setCreateTime(new Date());
					o.setUpdateTime(new Date());
					o.setIsWeek(1);
					o.setOrderMealGoodsId(id);
					o.setMealTypeId(Long.valueOf(mt));
					o.setPublicWeekDay(mw);
					saveObject(o);
				}
			}
		return R.ok();
	}

	/** 
	* @Title: updateNrsGoods 
	* @Description:  
	* @param @param nrsGoods    设定文件 
	* @return void    返回类型 
	* @throws 
	*/
	@Transactional(propagation = Propagation.REQUIRED,timeout=36000,rollbackFor=Exception.class)
	public void updateNrsGoods(NrsGoods nrsGoods) {
		mBaseDao.updateObject(nrsGoods);
	}


	/** 
	* @Title: deleteNrsGoods 
	* @Description:  
	* @param @param nrsGoods    设定文件 
	* @return void    返回类型 
	* @throws 
	*/
	@Transactional(propagation = Propagation.REQUIRED,timeout=36000,rollbackFor=Exception.class)
	public void deleteNrsGoods(NrsGoods nrsGoods) {
		mBaseDao.deleteObject(nrsGoods);
	}


}
